<?php


namespace EchoPHP\Database\Connectors;


use PDO;

class SqlServerConnector extends Connector implements ConnectorInterface {

    /**
     * 创建sqlserver连接
     * @param array $config
     * @return PDO
     */
    public function connect(array $config) {
        $dsn = $this->getDsn($config);
        $userName = $config['username'] ?? '';
        $password = $config['password'] ?? '';
        $options = $config['options'] ?? [];

        $options = $this->getOptions($options);
        return $this->createConnection($dsn, $userName, $password, $options);
    }

    /**
     * 获取dsn
     * @param array $config
     * @return string
     */
    protected function getDsn(array $config) {
        $dsn = 'sqlsrv:Server=' . $config['host'];
        if (isset($config['port']) && !empty($config['port'])) {
            $dsn .= ',' . $config['port'];
        }
        $dsn .= ';Database=' . $config['database'];
        return $dsn;
    }
}